package com.xl.rentkeeper.act.kanfang;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.RoomStatus;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.BaseAct;
import com.xl.rentkeeper.act.SearchRoomInfoAct;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.util.TipMapping;
import com.xl.rentkeeper.util.Util;
import com.xl.rentkeeper.view.TimePopupWindow;
import com.xl.rentkeeper.view.TitleBar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2015/8/17.
 */
public class AgainOrderRoomAct extends BaseAct implements View.OnClickListener {
    private TextView mTVName;
    private TextView mTVTime;
    private TextView mTVPrice;//金额
    private TextView mTVRemark;
    private ImageView mIVCall;
    private TextView mTVRoomName1;
    private TextView mTVRoomName2;
    private TextView mTVRoominfoStatus1;
    private TextView mTVRoominfoStatus2;
    private TextView mTVRoominfoPrice1;
    private TextView mTVRoominfoPrice2;
    private Order mOrder;
    RoomInfo[] roomInfos = new RoomInfo[2];


    private TimePopupWindow pwTime;
    private TextView mTVOrderTime;
    private long mTime = 0;
    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_again_room);

        TitleBar tb = (TitleBar) findViewById(R.id.tb);
        setTranslucentStatus();
        initTitleBar(tb);
        initView();
        initData();
        setTime();
    }

    private void initView() {
        mTVName = (TextView) findViewById(R.id.tv_name);
        mTVTime = (TextView) findViewById(R.id.tv_time);
        mTVPrice = (TextView) findViewById(R.id.tv_expect_price);
        mTVRemark = (TextView) findViewById(R.id.tv_remark);
        // mIVCall = (ImageView) findViewById(R.id.iv_call);

        mTVOrderTime = (TextView) findViewById(R.id.tv_again_orderTime);
        mTVRoomName1 = (TextView) findViewById(R.id.et_roomName1);
        mTVRoomName2 = (TextView) findViewById(R.id.et_roomName2);
        mTVRoominfoStatus1 = (TextView) findViewById(R.id.tv_roominfo_status1);
        mTVRoominfoStatus2 = (TextView) findViewById(R.id.tv_roominfo_status2);
        mTVRoominfoPrice1 = (TextView) findViewById(R.id.tv_price1);
        mTVRoominfoPrice2 = (TextView) findViewById(R.id.tv_price2);

        // mIVCall.setOnClickListener(this);
        mTVRoomName1.setOnClickListener(this);
        mTVRoomName2.setOnClickListener(this);
        mTVOrderTime.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = this.getIntent();
        mOrder = (Order) intent.getSerializableExtra("msg");

        mTVName.setText(mOrder.viewOrder.name);
        mTVPrice.setText(Util.replaceNull(mTVPrice, "￥", mOrder.viewOrder.expectPrice));
        mTVTime.setText(Util.replaceTimeNull(mOrder.viewOrder.viewTime));

        if(mOrder.viewOrder.viewTime == null|| mOrder.viewOrder.viewTime==0){
            mTVOrderTime.setText("");
        }else{
            mTVOrderTime.setText(Util.replaceTimeNull(mOrder.viewOrder.viewTime));
            mTime = mOrder.viewOrder.viewTime;
        }

        if(mOrder.viewOrder.viewRooms!=null && mOrder.viewOrder.viewRooms.size()!=0){
            if(mOrder.viewOrder.viewRooms.size()>0){
                RoomInfo rInfo = mOrder.viewOrder.viewRooms.get(0);
                roomInfos[0] = rInfo;
                if (TextUtils.isEmpty(rInfo.name)) {
                    mTVRoomName1.setText(Util.replaceNull(null, rInfo.name));
                } else {
                    mTVRoomName1.setText(Util.replaceNull(null, rInfo.name));
                }
                Util.setRoomStatus(mTVRoominfoStatus1, rInfo, "again_order");
                Util.setRoomPrice(mTVRoominfoPrice1,rInfo,"again_order");
            }
            if(mOrder.viewOrder.viewRooms.size()>1) {
                RoomInfo rInfo2 = mOrder.viewOrder.viewRooms.get(1);
                roomInfos[1] = rInfo2;
                if (TextUtils.isEmpty(rInfo2.name)) {
                    mTVRoomName2.setText(Util.replaceNull(null, rInfo2.name));
                } else {
                    mTVRoomName2.setText(Util.replaceNull(null, rInfo2.name));
                }
                Util.setRoomStatus(mTVRoominfoStatus2, rInfo2, "again_order");
                Util.setRoomPrice(mTVRoominfoPrice2,rInfo2,"again_order");
            }
        }
        mTVRemark.setText(Util.replaceNull(null, mOrder.viewOrder.remark));
    }

    public void submit(View v) {
        ArrayList<RoomInfo> rooms = new ArrayList<>();
        // String orderTime = mTVOrderTime.getText().toString().trim();
        if (mTime == 0) {
            App.showToast("请选择时间");
            return;
        }
        //String room1 = mTVRoomName1.getText().toString().trim();
        //String room2 = mTVRoomName2.getText().toString().trim();

        if (roomInfos[0] == null && roomInfos[1] == null) {
            App.showToast("请至少选择一个房间");
            return;
        } else {
            RoomInfo roomInfo;
            if (roomInfos[0] != null) {
                roomInfo = new RoomInfo.Builder().roomId(roomInfos[0].roomId).name(roomInfos[0].name).price(roomInfos[0].price).build();
                rooms.add(roomInfo);
            }
            if (roomInfos[1] != null) {
                roomInfo = new RoomInfo.Builder().roomId(roomInfos[1].roomId).name(roomInfos[1].name).price(roomInfos[1].price).build();
                rooms.add(roomInfo);
            }
        }
        mDialog = DialogUtil.showProgressDialog(this, R.string.loading);

        OrderLogic.getInstance().reView(mOrder.id, true, mTime, rooms, null);
    }

    private void setTime() {
        //tvTime=(TextView) findViewById(R.id.tvTime);
        //时间选择器
        pwTime = new TimePopupWindow(this, TimePopupWindow.Type.ALL);
        //时间选择后回调
        pwTime.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                mTime = date.getTime();
                mTVOrderTime.setText(getTime(date));
            }
        });
        //弹出时间选择器
        mTVOrderTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pwTime.showAtLocation(mTVOrderTime, Gravity.BOTTOM, 0, 0, new Date(mOrder.viewOrder.viewTime));
            }
        });

    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public String[] monitorEvents() {
        return new String[]{PMS_LOGIC_ReView};
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        super.onEvent(event, ret, errInfo, data);
        if (event.equals(OrderLogic.PMS_LOGIC_ReView)) {
            if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
            if (ret) {
                setResult(110);
                finish();
                // overridePendingTransition(R.anim.base_slide_remain, R.anim.base_slide_right_out);
            } else {
                App.showToast(errInfo);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.et_roomName1:
                intent = new Intent(this, SearchRoomInfoAct.class);
                intent.putExtra("position", 0);
                startActivityForResult(intent, 1);
                break;
            case R.id.et_roomName2:
                intent = new Intent(this, SearchRoomInfoAct.class);
                intent.putExtra("position", 1);
                startActivityForResult(intent, 1);
                break;
           /* case R.id.iv_call:
                intent = new Intent(this, SearchRoomInfoAct.class);
                intent.putExtra("position",1);
                startActivityForResult(intent,1);
                break;*/
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 || resultCode == 1) {
            if (data != null) {
                RoomInfo room = (RoomInfo) data.getSerializableExtra("info");
                int position = data.getIntExtra("position", 0);
                if(position==0){
                    if(roomInfos[1]!=null){
                        if(room.roomId.equals(roomInfos[1].roomId)){
                            App.showToast(R.string.select_room_repeat);
                            return;
                        }
                    }
                }else{
                    if(roomInfos[0]!=null){
                        if(room.roomId.equals(roomInfos[0].roomId)){
                            App.showToast(R.string.select_room_repeat);
                            return;
                        }
                    }
                }
                roomInfos[position] = room;
                refreshView(room, position);
            }
        }
    }

    private void refreshView(RoomInfo room, int position) {
        if (position == 0) {
            mTVRoomName1.setText(roomInfos[position].name);
            Util.setRoomStatus(mTVRoominfoStatus1, roomInfos[position], "again_order");
            Util.setRoomPrice(mTVRoominfoPrice1, roomInfos[position], "again_order");
        } else {
            mTVRoomName2.setText(roomInfos[position].name );
            Util.setRoomStatus(mTVRoominfoStatus2, roomInfos[position], "again_order");
            Util.setRoomPrice(mTVRoominfoPrice2, roomInfos[position], "again_order");
        }

    }
}
